<?php

/**
 * <b>UserAgent简介:</b>
 * <p>HTTP Head User-Agent对象模型。</p>
 *
 * <b>功能描述:</b>
 * <p>1. 获取浏览器功能数据。</p>
 * <p>2. 获取浏览器设备信息。</p>
 * <p>3. 识别浏览器相关特性，并针对不同浏览器做页面，用户体验适配。</p>
 *
 * @author cairq@ucweb.com
 */
class UserAgent {
    private $userAgent                          = "";	

    private $platform 				= "";
	
    private $encryption				= "";

    private $language				= "";
	
    private $os					= "";

    private $osVersion				= "";
	
    private $device				= "";
	
    private $renderingEngine			= "";

    private $renderingEngineVersion             = "";

    private $product				= "";
	
    private $productVersion			= "";
	
    private $ucPlatform                         = "";
	
    private $ucPlatformVersion                  = "";
	
    private $version                            = "";
	
    private $isMobileMode 			= false;
  	
    private $isUnkown                           = false;
	
    private $isEmpty                            = false;
	
    /**
     * 
     * <p>获取User-Agent的版本号，例如：新平台为：2.0.0，旧平台为：1.0.0</p>
     *
     * @return
     */
    public function getVersion() {
        return $this->version;
    }

    public function setVersion($version) {
        $this->version = $version;
    }
	    
    /**
     * 
     * <p>获取UC浏览器对应的平台，例如：U2，U3</p>
     *
     * @return
     */
    public function getUcPlatform() {
        return $this->ucPlatform;
    }

    public function setUcPlatform($ucPlatform) {
        $this->ucPlatform = $ucPlatform;
    }

    /**
     * 
     * <p>获取UC浏览器对应平台的版本，例如：0.8.0， 1.0.0</p>
     *
     * @return
     */
    public function getUcPlatformVersion() {
        return $this->ucPlatformVersion;
    }

    public function setUcPlatformVersion($ucPlatformVersion) {
        $this->ucPlatformVersion = $ucPlatformVersion;
    }

    /**
     * 
     * <p>检查当前UserAgent是否为空，即表示HTTP Header 没有User-Agent字段</p>
     *
     * @return
     */
    public function isEmpty() {
        return $this->isEmpty;
    }

    public function setEmpty($isEmpty) {
        $this->isEmpty = $isEmpty;
    }

    /**
     * 
     * <p>获取平台信息，例如：iOS，Linux，Windows,BlackBerry,Java,Symbian,MTK等</p>
     *
     * @return
     */
    public function getPlatform() {
        return $this->platform;
    }

    public function setPlatform($platform) {
        $this->platform = $platform;
    }

    /**
     * 
     * <p>获取加密信息，例如：U(128位加密)、I(40位加密)、N(没加密)</p>
     *
     * @return
     */
    public function getEncryption() {
        return $this->encryption;
    }

    public function setEncryption($encryption) {
        $this->encryption = $encryption;
    }
    
    /**
     * 
     * <p>获取语言信息，语言为小写，地区为大写，例如：zh-CN,en-US等</p>
     *
     * @return
     */
    public function getLanguage() {
        return $this->language;
    }

    public function setLanguage($language) {
        $this->language = $language;
    }

    /**
     * 
     * <p>获取操作系统，例如：S60，Android，iPhone，MIDP2.0，WP，BlackBerry，MTK，Brew等</p>
     *
     * @return
     */
    public function getOs() {
        return $this->os;
    }

    public function setOs($os) {
        $this->os = $os;
    }

    /**
     * 
     * <p>获取操作系统版本，例如：2.3,4.0,4.2等</p>
     *
     * @return
     */
    public function getOsVersion() {
        return $this->osVersion;
    }

    public function setOsVersion($osVersion) {
        $this->osVersion = $osVersion;
    }

    /**
     * 
     * <p>获取设备信息，例如：nokia6300，iPhone4等</p>
     *
     * @return
     */
    public function getDevice() {
        return $this->device;
    }

    public function setDevice($device) {
        $this->device = $device;
    }

    /**
     * 
     * <p>获取渲染引擎，例如：U2，U3，AppleWebKit，Gecko，Trident等</p>
     *
     * @return
     */
    public function getRenderingEngine() {
        return $this->renderingEngine;
    }

    
    public function setRenderingEngine($renderingEngine) {
        $this->renderingEngine = $renderingEngine;
    }

    /**
     * 
     * <p>获取渲染引擎版本号，例如：530.13，20100101等</p>
     *
     * @return
     */
    public function getRenderingEngineVersion() {
        return $this->renderingEngineVersion;
    }

    public function setRenderingEngineVersion($renderingEngineVersion) {
        $this->renderingEngineVersion = $renderingEngineVersion;
    }

    /**
     * 
     * <p>获取产品名称，例如：UCBrowser Safari等</p>
     *
     * @return
     */
    public function getProduct() {
        return $this->product;
    }

    public function setProduct($product) {
        $this->product = $product;
    }

    /**
     * 
     * <p>获取产品版本号，例如：8.3.1.202等</p>
     *
     * @return
     */
    public function getProductVersion() {
        return $this->productVersion;
    }

    public function setProductVersion($productVersion) {
        $this->productVersion = $productVersion;
    }

    /**
     * 
     * <p>当前模式是否适合手持设备浏览模式</p>
     *
     * @return
     */
    public function isMobileMode() {
        return $this->isMobileMode;
    }

    public function setMobileMode($isMobileMode) {
        $this->isMobileMode = $isMobileMode;
    }

    /**
     * 
     * <p>是否支持html5</p>
     *
     * @return
     */
    public function isSupperHtml5() {
        return isset($this->renderingEngine) && (stripos($this->renderingEngine, GlobalConstant::WEBKIT) !== false || stripos($this->renderingEngine, "trident") !== false);
    }
    
    /**
     * 
     * <p>是否WebKit渲染引擎</p>
     *
     * @return
     */
    public function isWebKit() {
        return isset($this->renderingEngine) && stripos($this->renderingEngine, GlobalConstant::WEBKIT) !== false;
    }
    
    /**
     * 
     * <p>是否安全模式浏览</p>
     * 
     * @return
     */
    public function isSafe(){
        return isset($this->encryption) && ($this->encryption == GlobalConstant::ENCRYPTION_U || $this->encryption == GlobalConstant::ENCRYPTION_I);
    }

    /**
     * 
     * <p>是否是U2内核</p>
     *
     * @return
     */
    public function isU2() {
        return isset($this->ucPlatform) && $this->ucPlatform == GlobalConstant::U2;
    }
    
    /**
     * 
     * <p>是否是U3内核</p>
     * 
     * 
     * @return
     */
    public function isU3() {
        return isset($this->ucPlatform) && $this->ucPlatform == GlobalConstant::U3;
    }
   
    /**
     * 
     * <p>是否是Symbian S60系统</p>
     *
     * @return
     */
    public function isS60(){
        return (isset($this->os) && (stripos($this->os, "s60") !== false)) || 
            (isset($this->platform) && (stripos($this->platform, "s60") !== false));
    }

    /**
     * 
     * <p>是否是Symbian S60 V1系统</p>
     *
     * @return
     */
    public function isS60V1(){
        return $this->isS60() && ((isset($this->osVersion) && stripos($this->osVersion, "v1") !== false) ||
                (isset($this->platform) && stripos($this->platform, "v1") !== false));
    }

    /**
     * 
     * <p>是否是Symbian S60 V2系统</p>
     *
     * @return
     */
    public function isS60V2(){
        return  $this->isS60() && ((isset($this->osVersion) && stripos($this->osVersion, "v2") !== false) ||
                (isset($this->platform) && stripos($this->platform, "v2") !== false));
    }

    /**
     * 
     * <p>是否是Symbian S60 V3系统</p>
     *
     * @return
     */
    public function isS60V3(){
        return $this->isS60() && ((isset($this->osVersion) && stripos($this->osVersion, "v3") !== false) ||
                (isset($this->platform) && stripos($this->platform, "v3") !== false));
    }

    /**
     * 
     * <p>是否是Symbian S60 V5系统</p>
     *
     * @return
     */
    public function isS60V5(){
        return $this->isS60() && ((isset($this->osVersion) && stripos($this->osVersion, "v5") !== false) ||
                (isset($this->platform) && stripos($this->platform, "v5") !== false));
    }

    /**
     * 
     * <p>是否是Symbian3系统</p>
     *
     * @return
     */
    public function isSymbian3(){
        return (isset($this->os) && (stripos($this->os, "Symbian3") !== false )) ||
                ( isset($this->platform) && (stripos($this->platform, "Symbian3") !== false ));
    }

    /**
     * 
     * <p>是否是iPhone</p>
     *
     * @return
     */
    public function isiPhone(){
        return (isset($this->device) && (stripos($this->device, "iphone") !== false || stripos($this->device, "iph") !== false)) || (isset($this->os) && (stripos($this->os, "iphone") !== false || stripos($this->os, "iph") !== false));
    }

    /**
     * 
     * <p>是否是iPhone1</p>
     *
     * @return
     */
    public function isiPhone1(){
        return isset($this->device) && (stripos($this->device, "iphone1") !== false || stripos($this->device, "iph1") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是iPhone2</p>
     *
     * @return
     */
    public function isiPhone2(){
        return isset($this->device) && (stripos($this->device, "iphone2") !== false || stripos($this->device, "iph2") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是iPhone3</p>
     *
     * @return
     */
    public function isiPhone3(){
        return isset($this->device) && (stripos($this->device, "iphone3") !== false || stripos($this->device, "iph3") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是iPhone4</p>
     *
     * @return
     */
    public function isiPhone4(){
        return isset($this->device) && (stripos($this->device, "iphone4") !== false || stripos($this->device, "iph4") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是iPhone5</p>
     *
     * @return
     */
    public function isiPhone5(){
        return isset($this->device) && (stripos($this->device, "iphone5") !== false || stripos($this->device, "iph5") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是iPad</p>
     *
     * @return
     */
    public function isiPad(){
        return isset($this->device) && (stripos($this->device, "ipad") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是Windows Phone</p>
     *
     * @return
     */
    public function isWP(){
        return isset($this->os) && (stripos($this->os, "wp") !== false || stripos($this->os, "windows phone") !== false) ;
    } 
    
    /**
     * 
     * <p>是否是Android设备
     * </p>
     *
     * @return
     */
    public function isAndroid(){
        return isset($this->os) && (stripos($this->os, "android") !== false || stripos($this->os, "adr") !== false);
    } 
    
    /**
     * 
     * <p>是否是iPhone</p>
     *
     * @return
     */
    public function isApad(){
        return false;
    } 
    
    /**
     * 
     * <p>是否是WinPad</p>
     *
     * @return
     */
    public function isWinPad(){
        return false;
    }
	
    /**
     * 
     * <p>是否是智能设备</p>
     *
     * @return
     */
    public function isSmartPhone(){
        return !($this->isJava() || $this->isMTK() || $this->isBrew());
    }
    
    /**
     * 
     * <p>是否是Brew设备</p>
     *
     * @return
     */
    public function isBrew(){
        return isset($this->platform) && stripos($this->platform, "brew") !== false;
    }
    
    /**
     * 
     * <p>是否是Java平台</p>
     *
     * @return
     */
    public function isJava(){
        return isset($this->platform) && stripos($this->platform, GlobalConstant::JAVA) !== false;
    }
    
    /**
     * 
     * <p>是否是MTK设备</p>
     *
     * @return
     */
    public function isMTK(){
        return isset($this->platform) && stripos($this->platform, "mtk") !== false;
    }
    
    /**
     * 
     * <p>是否是Linux平台</p>
     *
     * @return
     */
    public function isLinux(){
        return isset($this->platform) && stripos($this->platform, "linux") !== false;
    }
    
    /**
     * 
     * <p>是否是Windows平台</p>
     *
     * @return
     */
    public function isWindows(){
        return isset($this->platform) && stripos($this->platform, "windows") !== false;
    }
    
    /**
     * 
     * <p>是否是Symbian平台</p>
     *
     * @return
     */
    public function isSymbian(){
        return $this->isS60() || isset($this->platform) && stripos($this->platform, "symbian") !== false;
    }
    
    /**
     * 
     * <p>是否是iOS平台</p>
     *
     * @return
     */
    public function isiOS(){
        return isset($this->platform) && stripos($this->platform, "ios") !== false;
    }

    /**
     * 
     * <p>检查当前UserAgent是否未知，一般情况为无法识别</p>
     *
     * @return
     */
    public function isUnkown() {
        return $this->isUnkown;
    }

    public function setUnkown($isUnkown) {
        $this->isUnkown = $isUnkown;
    }

    /**
     * 
     * <p>检查当前UserAgent是否旧UA格式</p>
     *
     * @return
     */
    public function isOldVersion() {
        return isset($this->version) && strpos($this->version, GlobalConstant::VERSION_1) !== false;
    }

    /**
     * 
     * <p>检查当前UserAgent是否新UA格式</p>
     *
     * @return
     */
    public function isNewVersion() {
        return !$this->isOldVersion();
    }
    
    /**
     * 
     * <p>获取原始的HTTP Header字段</p>
     *
     * @return
     */
    public function getUserAgent() {
        return $this->userAgent;
    }

    public function setUserAgent($userAgent) {
        $this->userAgent = $userAgent;
    }
    
    public static function toBoolValue($value){
        return $value ? "true" : "false";
    }
    
    public function toString(){
        $sb  = "Platform:                        " . $this->platform . "\r\n";
        $sb .= "Encryption:                      " . $this->encryption . "\r\n";
        $sb .= "Language:                        " . $this->language . "\r\n";
        $sb .= "OS:                              " . $this->os . "\r\n";
        $sb .= "OS Version                       " . $this->osVersion . "\r\n";
        $sb .= "Device:                          " . $this->device . "\r\n";
        $sb .= "Rendering Engine:                " . $this->renderingEngine . "\r\n";
        $sb .= "Rendering Engine Version:        " . $this->renderingEngineVersion . "\r\n";
        $sb .= "Product:                         " . $this->product . "\r\n";
        $sb .= "Product Version:                 " . $this->productVersion . "\r\n";
        $sb .= "UC Platform:                     " . $this->ucPlatform . "\r\n";
        $sb .= "UC Platform Version:             " . $this->ucPlatformVersion . "\r\n";
        $sb .= "isUnkown:                        " . UserAgent::toBoolValue($this->isUnkown()) . "\r\n";     
        $sb .= "isSupperHtml5:                   " . UserAgent::toBoolValue($this->isSupperHtml5()) . "\r\n";   
        $sb .= "isSafe:                          " . UserAgent::toBoolValue($this->isSafe()) . "\r\n";   
        $sb .= "isU2:                            " . UserAgent::toBoolValue($this->isU2()) . "\r\n";   
        $sb .= "isU3:                            " . UserAgent::toBoolValue($this->isU3()) . "\r\n";   
        $sb .= "isS60:                           " . UserAgent::toBoolValue($this->isS60()) . "\r\n";   
        $sb .= "isS60V1:                         " . UserAgent::toBoolValue($this->isS60V1()) . "\r\n";   
        $sb .= "isS60V2:                         " . UserAgent::toBoolValue($this->isS60V2()) . "\r\n";   
        $sb .= "isS60V3:                         " . UserAgent::toBoolValue($this->isS60V3()) . "\r\n";   
        $sb .= "isS60V5:                         " . UserAgent::toBoolValue($this->isS60V5()) . "\r\n";   
        $sb .= "isSymbian3:                      " . UserAgent::toBoolValue($this->isSymbian3()) . "\r\n";   
        $sb .= "isiPhone:                        " . UserAgent::toBoolValue($this->isiPhone()) . "\r\n";   
        $sb .= "isiPhone1:                       " . UserAgent::toBoolValue($this->isiPhone1()) . "\r\n";   
        $sb .= "isiPhone2:                       " . UserAgent::toBoolValue($this->isiPhone2()) . "\r\n";   
        $sb .= "isiPhone3:                       " . UserAgent::toBoolValue($this->isiPhone3()) . "\r\n";   
        $sb .= "isiPhone4:                       " . UserAgent::toBoolValue($this->isiPhone4()) . "\r\n";   
        $sb .= "isiPhone5:                       " . UserAgent::toBoolValue($this->isiPhone5()) . "\r\n";   
        $sb .= "isiPad:                          " . UserAgent::toBoolValue($this->isiPad()) . "\r\n";   
        $sb .= "isWP:                            " . UserAgent::toBoolValue($this->isWP()) . "\r\n";   
        $sb .= "isAndroid:                       " . UserAgent::toBoolValue($this->isAndroid()) . "\r\n";   
        $sb .= "isApad:                          " . UserAgent::toBoolValue($this->isApad()) . "\r\n";   
        $sb .= "isWinPad:                        " . UserAgent::toBoolValue($this->isWinPad()) . "\r\n";   
        $sb .= "isSmartPhone:                    " . UserAgent::toBoolValue($this->isSmartPhone()) . "\r\n";   
        $sb .= "isBrew:                          " . UserAgent::toBoolValue($this->isBrew()) . "\r\n";   
        $sb .= "isJava:                          " . UserAgent::toBoolValue($this->isJava()) . "\r\n";   
        $sb .= "isMTK:                           " . UserAgent::toBoolValue($this->isMTK()) . "\r\n";   
        $sb .= "isLinux:                         " . UserAgent::toBoolValue($this->isLinux()) . "\r\n";   
        $sb .= "isWindows:                       " . UserAgent::toBoolValue($this->isWindows()) . "\r\n";   
        $sb .= "isiOS:                           " . UserAgent::toBoolValue($this->isiOS()) . "\r\n";   
        $sb .= "isEmpty:                         " . UserAgent::toBoolValue($this->isEmpty()) . "\r\n";   
        $sb .= "isSymbian:                       " . UserAgent::toBoolValue($this->isSymbian()) . "\r\n";    
        return $sb;
    }
}
?>
